package com.product.system.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.product.system.domain.request.ArgpMachiningSerch;
import com.product.system.domain.response.ArgpMachining;
import com.product.system.domain.response.ArgpTradesmanCity;
import com.product.system.listener.ArgpMachiningDataListener;
import com.product.system.service.ArgpMachiningService;
import com.product.system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("machining")
public class ArgpMachiningController {



    @Autowired
    ArgpMachiningService argpMachiningService;


    @GetMapping("machiningList")
    public Result machiningList(ArgpMachiningSerch argpMachiningSerch) {
        PageInfo<ArgpMachining> info =  argpMachiningService.machiningList(argpMachiningSerch);
       return Result.success(info);
    }

    @PostMapping("machiningAdd")
    public Result machiningAdd(@RequestBody ArgpMachining argpMachining) {
        int i = argpMachiningService.machiningAdd(argpMachining);
        return Result.success(i);

    }

    @GetMapping("excelPort")
    public void downLoad(HttpServletResponse response) throws IOException {
        List<ArgpMachining> argpMachiningList = argpMachiningService.machiningExcelList();
        //指定类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        //指定文件名称
        String fileName = URLEncoder.encode("加工信息", "UTF-8").replaceAll("\\+", "%20");
        //指定响应头
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        EasyExcel.write(response.getOutputStream(), ArgpMachining.class).sheet("sheet1").doWrite(argpMachiningList);

    }



    @PostMapping("loadinExcel")
    public Result loadinExcel(MultipartFile file) throws IOException {
        try {
            EasyExcel.read(file.getInputStream(), ArgpMachining.class, new ArgpMachiningDataListener(argpMachiningService)).doReadAll();
            return Result.success();
        } catch (Exception aa) {
            aa.printStackTrace();
            return Result.fail();
        }
    }

    @GetMapping("getOptionsTreeList")
    public Result getOptionsTreeList() {

      List<ArgpTradesmanCity> list =   argpMachiningService.getOptionTreeList();

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<ArgpTradesmanCity, String>build(list, "1", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("type",treeNode.getLevel()+"");
                });

      return Result.success(treeNodes);
    }

    @GetMapping("machiningDel")
    public Result machiningDel(Integer id) {
        int i = argpMachiningService.machiningDel(id);
        return Result.success(i);
    }



}
